package com.chinacoal.microservice.impl.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Element;

import com.baomidou.mybatisplus.plugins.Page;

/**  
 * <p>Title: commonFun</p>  
 * <p>Description: 公用方法</p>  
 * @author sunh
 * @date 2019年9月11日  下午4:24:30
 */
public class UtaskCommonFun {
	
	/**
	 * <p>Title: getPage</p>
	 * <p> Description: 获取分页组件 </p>
	 * @param <T>
	 * @param page
	 * @param pageSize
	 * @return
	 */
	public static <T> Page<T> getPage(Integer page, Integer pageSize) {

		if (page == null || pageSize == null || page < 1 || pageSize < 1)
			return new Page<T>(0, Integer.MAX_VALUE);

		return new Page<T>(page, pageSize);
	}
	
	/**  
	 * @Package: com.chinacoal.microservice.impl.service.impl.SysConvertServiceImpl.getXmlMap
	 * @MethodName: getXmlMap
	 * @MethodDesc: xml转换Map
	 * @ParamList: {
	 *				 @param element
	 *			   }
	 * @Return: Map<String,Object>
	 * @Createby: sunh
	 * @CreatedDate: 2020-01-10 16:42:11
	 */ 
	public static Map<String, Object> xmlToMap(Element element){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		for (Iterator<Element> iterable = element.elementIterator(); iterable.hasNext();) {
			Element nodeElement = iterable.next();
			String key = nodeElement.getName();
			String value = nodeElement.getText();
			resultMap.put(key, value);
		}
		
		return resultMap;
	}
	
	/**  
	 * @Package: com.chinacoal.microservice.impl.util.CommonFun.xmlToList
	 * @MethodName: xmlToList
	 * @MethodDesc: xml转换List
	 * @ParamList: {
	 *				 @param element
	 *			   }
	 * @Return: List<Map<String,Object>>
	 * @Createby: sunh
	 * @CreatedDate: 2020-01-10 16:48:52
	 */ 
	public static List<Map<String, Object>> xmlToList(Element element){
		
		List<Map<String, Object>> resultList = new ArrayList<Map<String,Object>>();
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		for (Iterator<Element> iterable = element.elementIterator(); iterable.hasNext();) {
			Element nodeElement = iterable.next();
			String key = nodeElement.attributeValue("name");
			String value = nodeElement.attributeValue("value");
						
		}
		
		return resultList;
	}
	
	/**  
	 * @Package: com.chinacoal.microservice.impl.util.CommonFun.mapToXml
	 * @MethodName: mapToXml
	 * @MethodDesc: 
	 * @ParamList: {
	 *				 @param element
	 *			   }
	 * @Return: Map<String,Object>
	 * @Createby: sunh
	 * @CreatedDate: 2020-01-14 16:17:41
	 */ 
	public static String mapToXml(Map<String, Object> param){
		
		if(param == null || param.size() <1) return null;
		
		String result = "";
		
		for(Map.Entry<String, Object> map : param.entrySet()){
			result =  result + "<" + map.getKey() + ">" + map.getValue() + "</" + map.getKey() + ">";
		}
		
		return result;
	}
}
